﻿using AutoMapper;
using Enter.Admin.Dtoes;
using Enter.Admin.Entities;
using Enter.Admin.Filters;
using Enter.Admin.Models;
using Enter.Admin.Models.DataTables;
using Enter.Admin.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Enter.Admin.Controllers
{
    [TypeFilter(typeof(ActionFilter), Arguments = new object[] { ActionRole.Admin })]
    public class TemplateController : BaseController
    {

        public TemplateController(ILogger<TemplateController> logger, IFreeSql fsql, IMapper mapper) : base(logger, fsql, mapper)
        {
        }
        public IActionResult Index()
        {
            return View();
        }
        public ActionResult Template()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index([FromBody] DataTablesParameters<string> model)
        {
            var data = _fsql.Select<DynamicTable>().ToList();
            return Json(new DataTablesResult<DynamicTable>(model.Draw, data.Count, data.Count, data));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(long TableId,string Name)
        {
            if (TableId > 0)
            {
                _fsql.Update<DynamicTable>().Set(t => t.Name, Name).Where(t => t.Id == TableId).ExecuteAffrows();
            }
            else 
            {
                var session = HttpContext.Session.Get<Employ>();
                _fsql.Insert<DynamicTable>(new DynamicTable
                {
                    Id = 0,
                    UserId = session.Id,
                    Name = Name,
                    TableName = nameof(Archive),
                }).ExecuteAffrows();
            }
            BaseController.UpdateDynamicTableList(_fsql);
            return Json(new ResponseResult<string>(true, "修改成功."));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(long id)
        {
            if (id > 1)
            {
                _fsql.Transaction(() =>
                {
                    _fsql.Delete<DynamicTable>().Where(t => t.Id == id).ExecuteAffrows();
                    _fsql.Delete<DynamicColumn>().Where(t => t.TableId == id).ExecuteAffrows();
                });
            }
            BaseController.UpdateDynamicTableList(_fsql);
            return Json(new ResponseResult<string>(true, "删除成功."));
        }

        [HttpPost]
        public ActionResult Items(long tableId)
        {
            var data = _fsql.Select<DynamicColumn>().Where(t=>t.TableId == tableId ).OrderBy(t=>t.Sort).ToList();
            return Json(new ResponseResult<List<DynamicColumn>>(true, data, "ok."));
        }

        [HttpPost]
        public ActionResult AddItems([FromBody]AddColumnModel model)
        {
            _fsql.Transaction(() =>
            {
                _fsql.Delete<DynamicColumn>().Where(t => t.TableId == model.TableId).ExecuteAffrows();
                var i = 0;
                foreach (var column in model.Columns)
                {
                    i++;
                    _fsql.Insert<DynamicColumn>(new DynamicColumn
                    {
                        Id = 0,
                        TableId = model.TableId,
                        ColumnName = column,
                        Sort =i,
                    }).ExecuteAffrows();
                }
            });
            return Json(new ResponseResult<string>(true, "更新成功."));
        }
        

    }
}
